/*
 * File: ProgressBar.cs
 * Author: Gourav Das
 * Purpose: Basic Progress Bar Implementation
 * Created: May 01 2011
 * Copyright: Gourav Das, 2011
 */

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace _22yards.Helpers
{
    /// <summary>
    /// Progress Bar class
    /// </summary>
    public class ProgressBar// : Microsoft.Xna.Framework.DrawableGameComponent
    {
        ContentManager content;
        Texture2D backgroundTexture;
        Texture2D barTexture;
        /// <summary>
        /// Gets or sets the sprite batch.
        /// </summary>
        /// <value>
        /// The sprite batch.
        /// </value>
        public SpriteBatch spriteBatch { get; set; }

        /// <summary>
        /// Gets or sets the helper text.
        /// </summary>
        /// <value>
        /// The helper text.
        /// </value>
        public string HelperText { get; set; }
        /// <summary>
        /// Gets or sets a value indicating whether [show text].
        /// </summary>
        /// <value>
        ///   <c>true</c> if [show text]; otherwise, <c>false</c>.
        /// </value>
        public bool ShowText { get ; set ; }

        private float privalue;
        /// <summary>
        /// Gets or sets the value.
        /// </summary>
        /// <value>
        /// The value.
        /// </value>
        public float Value
        {
            get { return privalue; }
            set
            {
                if (Minimum <= value && value <= Maximum)
                {
                    privalue = value;
                }
            }
        }
        /// <summary>
        /// Gets or sets the minimum.
        /// </summary>
        /// <value>
        /// The minimum.
        /// </value>
        public float Minimum { get; set; }
        /// <summary>
        /// Gets or sets the maximum.
        /// </summary>
        /// <value>
        /// The maximum.
        /// </value>
        public float Maximum { get; set; }

        /// <summary>
        /// Gets or sets the color of the background.
        /// </summary>
        /// <value>
        /// The color of the background.
        /// </value>
        public Color BackgroundColor { get; set; }
        /// <summary>
        /// Gets or sets the color of the primary.
        /// </summary>
        /// <value>
        /// The color of the primary.
        /// </value>
        public Color PrimaryColor { get; set; }
        ///// <summary>
        ///// Gets or sets the color of the secondary.
        ///// </summary>
        ///// <value>
        ///// The color of the secondary.
        ///// </value>
        //public Color SecondaryColor { get; set; }
        /// <summary>
        /// Gets or sets the position.
        /// </summary>
        /// <value>
        /// The position.
        /// </value>
        public Vector2 Position { get; set; }
        /// <summary>
        /// Gets or sets the font.
        /// </summary>
        /// <value>
        /// The font.
        /// </value>
        public SpriteFont Font { get; set; }

        /// <summary>
        /// Gets or sets the height.
        /// </summary>
        /// <value>
        /// The height.
        /// </value>
        public int Height { get; set; }
        /// <summary>
        /// Gets or sets the width.
        /// </summary>
        /// <value>
        /// The width.
        /// </value>
        public int Width { get; set; }
        /// <summary>
        /// Gets or sets the graphics device.
        /// </summary>
        /// <value>
        /// The graphics device.
        /// </value>
        public GraphicsDevice graphicsDevice { get; set; }

        /// <summary>
        /// Initializes a new instance of the <see cref="ProgressBar"/> class.
        /// </summary>
        /// <param name="_game">The _game.</param>
        /// <param name="_helpertext">The _helpertext.</param>
        public ProgressBar(Game _game, string _helpertext="")
            //: base(_game)
        {
            content = new ContentManager(_game.Services, "22yardsContent");
            HelperText = _helpertext;
            Height = 16;
            Width = 128;
            BackgroundColor = Color.White;
            PrimaryColor = Color.White;
            //SecondaryColor = Color.Red;
            Position = Vector2.Zero;
            graphicsDevice = _game.GraphicsDevice;
            spriteBatch = new SpriteBatch(graphicsDevice);
            backgroundTexture = content.Load<Texture2D>("Helpers/Texture/PBBackground");
            barTexture = content.Load<Texture2D>("Helpers/Texture/PBTexture");
            Font = content.Load<SpriteFont>("Helpers/Fonts/ProgressBarFont");
            Minimum = 0;
            Maximum = 100;
            Value = 0;
        }

        ///// <summary>
        ///// Called when graphics resources need to be loaded. Override this method to load any component-specific graphics resources.
        ///// </summary>
        //protected void LoadContent()
        //{
        //    spriteBatch = new SpriteBatch(graphicsDevice);
        //    backgroundTexture = content.Load<Texture2D>("Helpers/Texture/ProgressBarbg");
        //    barTexture = content.Load<Texture2D>("Helpers/Texture/ProgressBarbg");
        //    Font = content.Load<SpriteFont>("Helpers/Fonts/ProgressBarFont");
        //}

        /// <summary>
        /// Called when the GameComponent needs to be updated. Override this method with component-specific update code.
        /// </summary>
        /// <param name="gameTime">Time elapsed since the last call to Update</param>
        public void Update(GameTime gameTime)
        {
            //base.Update(gameTime);
        }


        /// <summary>
        /// Called when the DrawableGameComponent needs to be drawn. Override this method with component-specific drawing code. Reference page contains links to related conceptual articles.
        /// </summary>
        /// <param name="horscale">The scale.</param>
        /// <param name="vertscale">The vertscale.</param>
        /// <param name="font">The font.</param>
        public void Draw(int horscale=0, int vertscale=0, SpriteFont font=null/*GameTime gameTime*/)
        {
            //spriteBatch.Begin();
            spriteBatch.Draw(backgroundTexture, new Rectangle((int)Position.X, (int)Position.Y, Width+horscale, Height+vertscale), BackgroundColor);
        
            //int BarWidthSec = (int)(Width / Maximum * Value);
            //spriteBatch.Draw(barTexture, new Rectangle((int)Position.X, (int)Position.Y, BarWidthSec, 5), SecondaryColor);
            
            int BarWidthPri = (int)(((Width+horscale)*privalue) / Maximum);
            spriteBatch.Draw(barTexture, new Rectangle((int)Position.X, (int)Position.Y, BarWidthPri, Height + vertscale), PrimaryColor);
            
            if (font != null && ShowText)
            {
                Vector2 stringSize = Font.MeasureString(HelperText  + ":" + privalue.ToString());
                spriteBatch.DrawString(font, HelperText + ":" + privalue.ToString(), new Vector2((Position.X + Width+horscale) / 2 - (stringSize.X / 2), Position.Y - 5f), Color.Black,0f,Vector2.Zero,2.3f,SpriteEffects.None, 0);
            }

            //spriteBatch.End();
        }

        /// <summary>
        /// Increases the specified amount.
        /// </summary>
        /// <param name="amount">The amount.</param>
        public void Increase(float amount)
        {
            if (amount <= (Maximum - privalue))
            {
                privalue += amount;
            }
            else
            {
                privalue = Maximum;
            }
        }

        /// <summary>
        /// Decreases the specified amount.
        /// </summary>
        /// <param name="amount">The amount.</param>
        public void Decrease(float amount)
        {
            if (amount <= privalue)
            {
                privalue -= amount;
            }
            else
            {
                privalue = Minimum;
            }
        }
    }
}
